/**
 * 
 */
package com.study.io.netty;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;

/**
 * @author Administrator
 * @date 2019年6月9日
 */

public class ClientBootStrap {
    public static void main(String[] args) throws InterruptedException {
        //1.Create Bootstrap
        Bootstrap bt = new Bootstrap();
        //2.Create thread pool(worker)
        EventLoopGroup worker = new NioEventLoopGroup();
        //3.关联线程池
        bt.group(worker);
        //4.设置通道
        bt.channel(NioSocketChannel.class);
        //5.添加Handler
        bt.handler(new ClientChannelInitializer());

      //6.建立连接
        ChannelFuture channelFuture = bt.connect("127.0.0.1",9999);
        try {
            //7.测试输入
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            while(true){
                System.out.println("请输入：");
                String msg = bufferedReader.readLine();
                channelFuture.channel().writeAndFlush(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            //8.关闭连接
            worker.shutdownGracefully();
        }
    }
}

